﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Constants;
using MonkeyWarranty.Web.Common;
using Resources;

namespace MonkeyWarranty.Web.Employees
{
    public partial class RoleEmployee : System.Web.UI.Page
    {
        #region Properties

        private int _roleId;

        private int RoleId
        {
            get
            {
                if (_roleId == 0)
                {
                    int.TryParse(Request["roleId"], out _roleId);
                }
                return _roleId;
            }
        }

        private EmployeeBiz _employeeBiz;

        private EmployeeBiz EmployeeManager
        {
            get { return _employeeBiz ?? (_employeeBiz = new EmployeeBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WarrantyCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/Logon.aspx");
                return;
            }

            if (!WarrantyCommon.CurrrentEmployee.HasRole(RoleConstant.Admin))
            {
                Response.Redirect("~/NoPermission.aspx");
            }

            if (!IsPostBack)
            {
                if (RoleId <= 0)
                {
                    return;
                }

                LoadRoleData();

                // Load employees have role
                LoadEmployees();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            // get assigned employees Ids
            var assignedIds = new List<int>();
            foreach (ListItem item in ListBox2.Items)
            {
                int id;
                if (int.TryParse(item.Value, out id))
                {
                    assignedIds.Add(id);
                }
            }

            EmployeeManager.AssignToRole(RoleId, assignedIds);

            errorMessage.Visible = true;
            lblErrorMessage.Text = GlobalStrings.UpdateSuccess;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            errorMessage.Visible = false;

            for (var i = ListBox1.Items.Count - 1; i >= 0; i--)
            {
                if (ListBox1.Items[i].Selected)
                {
                    ListBox2.Items.Add(ListBox1.Items[i]);
                    ListBox2.ClearSelection();
                    ListBox1.Items.RemoveAt(i);
                }
            }
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            errorMessage.Visible = false;

            for (var i = ListBox2.Items.Count - 1; i >= 0; i--)
            {
                if (ListBox2.Items[i].Selected)
                {
                    ListBox1.Items.Add(ListBox2.Items[i]);
                    ListBox1.ClearSelection();
                    ListBox2.Items.RemoveAt(i);
                }
            }
        }

        #endregion

        #region Helpers

        private void LoadEmployees()
        {
            var employees1 = EmployeeManager.GetEmployeesOfRole(RoleId);
            var roledIds = new List<int>();
            if (employees1 != null && employees1.Count > 0)
            {
                ListBox2.DataSource = employees1;
                ListBox2.DataValueField = "Id";
                ListBox2.DataTextField = "Name";
                ListBox2.DataBind();

                roledIds.AddRange(employees1.Select(employee => employee.Id));
            }

            var employees = EmployeeManager.GetAllEmployees();
            var employees2 = employees.Where(x => x.Id > 1 && !roledIds.Contains(x.Id)).ToList();
            if (employees2.Count > 0)
            {
                ListBox1.DataSource = employees2;
                ListBox1.DataValueField = "Id";
                ListBox1.DataTextField = "Name";
                ListBox1.DataBind();
            }
        }

        private void LoadRoleData()
        {
            var role = EmployeeManager.GetRoleById(RoleId);
            if (role != null)
            {
                ltrRole.Text = string.Format("{0} ({1})", role.Name, role.Description);
            }
        }

        #endregion
    }
}